package com.example.sunflowers.adapters

import android.text.method.LinkMovementMethod
import android.widget.ImageView
import android.widget.TextView
import androidx.core.text.HtmlCompat
import androidx.core.text.HtmlCompat.FROM_HTML_MODE_COMPACT
import androidx.databinding.BindingAdapter
import cn.trans88.taxiappkotlin.ext.logd
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.example.sunflowers.R
import com.google.android.material.floatingactionbutton.FloatingActionButton

/**
 *
 * @Author： LJH
 * @Time： 2024/1/27
 * @description：
 */
@BindingAdapter("imageFromUrl")
fun bindImageFromUrl(view:ImageView,imageUrl:String?){
    "Image from url:$imageUrl".logd()
    val defaultUrl = "http://82.157.163.217/404.png"
    if (!imageUrl.isNullOrEmpty()){
        Glide.with(view.context)
            .load(imageUrl)
            .transition(DrawableTransitionOptions.withCrossFade())
            .into(view)
    } else {
        Glide.with(view.context)
            .load(defaultUrl)
            .transition(DrawableTransitionOptions.withCrossFade())
            .into(view)
    }
}

@BindingAdapter("isFabGone")
fun bindIsFabGone(view:FloatingActionButton,isGone:Boolean){
    if (isGone == null || isGone){
        view.hide()
    } else {
        view.show()
    }
}

@BindingAdapter("renderHtml")
fun bindRenderHtml(view:TextView,description:String?){
    if (description != null){
        view.text = HtmlCompat.fromHtml(description,FROM_HTML_MODE_COMPACT)
        view.movementMethod = LinkMovementMethod.getInstance()
    } else {
        view.text = ""
    }
}

@BindingAdapter("wateringText")
fun bindWateringText(textView: TextView,waterInterval:Int){
    val resources = textView.context.resources
    val quantityString = resources.getQuantityString(
        R.plurals.watering_needs_suffix,
        waterInterval,
        waterInterval
    )
    textView.text = quantityString
}